/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.gis.eemc.dao;

import net.gis.eemc.domain.Faculty;
import net.gis.eemc.domain.Department;
import net.gis.eemc.domain.Discipline;
import net.gis.eemc.domain.HibernateUtil;

import java.util.List;
import java.util.ArrayList;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author Sergey
 */
public class DisciplineDAOImpl implements DisciplineDAO {

    Session session = null;

    @Override
    public void addDiscipline(Discipline discipline) {
        org.hibernate.Transaction tx = null;

        try {
            this.session = HibernateUtil.getSessionFactory().getCurrentSession();

            tx = session.beginTransaction();
            session.save(discipline);
            tx.commit();
        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        }
    }

    @Override
    public void updateDiscipline(Discipline discipline) {
        org.hibernate.Transaction tx = null;
        this.session = HibernateUtil.getSessionFactory().getCurrentSession();
        try {
            tx = session.beginTransaction();
            session.update(discipline);
            tx.commit();
        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        }
    }

    @Override
    public void removeDiscipline(Discipline discipline) {
        org.hibernate.Transaction tx = null;
        this.session = HibernateUtil.getSessionFactory().getCurrentSession();
        try {
            tx = session.beginTransaction();
            session.delete(discipline);
            tx.commit();
        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        }
    }

    @Override
    public List<Discipline> listDisciplines() {
        return getDisciplines("from Discipline as discipline");
    }

    @Override
    public List<Discipline> listDisciplines(Faculty faculty) {
        org.hibernate.Transaction tx = null;
        List<Discipline> disciplines = new ArrayList<Discipline>();

        try {
            this.session = HibernateUtil.getSessionFactory().getCurrentSession();
            /*Hibernate.initialize(fac);
            Hibernate.initialize(fac.getDepartments());*/

            tx = session.beginTransaction();
            session.refresh(faculty);
            for (Department dep : faculty.getDepartments()) {
                disciplines.addAll(listDisciplines(dep));
            }

            //tx.commit();
        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        }

        return disciplines;
    }

    @Override
    public List<Discipline> listDisciplines(Department department) {
        return listDisciplines(department.getIdDep());
    }

    private List<Discipline> listDisciplines(Integer depId) {
        return getDisciplines("from Discipline as discipline where discipline.department.idDep=\"" + depId.toString() + "\"");
    }
    
    @Override
    public Discipline getDiscipline(Integer discipId) {
        List<Discipline> disciplines = getDisciplines("from Discipline as discipline where discipline.id=\"" + discipId.toString() + "\"");
        return disciplines.get(0);
    }

    @Override
    public List<Discipline> getDisciplinesWithName(String disciplineName) {
        return getDisciplines("from Discipline as discipline where discipline.disciplineName=\"" + disciplineName + "\"");
    }

    @Override
    public List<Discipline> getDisciplinesWithCode(int disciplineCode) {
        return getDisciplines("from Discipline as discipline where discipline.code=\"" + disciplineCode + "\"");
    }

    private List<Discipline> getDisciplines(String query) {
        List<Discipline> disciplinesList = null;
        org.hibernate.Transaction tx = null;

        try {
            this.session = HibernateUtil.getSessionFactory().getCurrentSession();

            tx = session.beginTransaction();
            Query q = session.createQuery(query);
            disciplinesList = (List<Discipline>) q.list();
            tx.commit();
        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        }
        return disciplinesList;
    }
}
